package wangjg.xuexiban.util;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map.Entry;

import android.graphics.Bitmap;

public class ResoureCache {
	
	public ResoureCache(){
		
	}
	
//	private WeakHashMap<String,Bitmap> map = new WeakHashMap<String,Bitmap>();
	
	private HashMap<String,SoftReference<Bitmap>> map = new HashMap<String,SoftReference<Bitmap>>();
	
	public Bitmap get(String path){
		Bitmap bitmap = null;
		SoftReference<Bitmap> sr = map.get(path);
		if(sr==null || sr.get()==null){
			bitmap = ResoureUtil.getImageResoure(path);
			map.put(path, new SoftReference<Bitmap>(bitmap));
		}else{
			bitmap = sr.get();
		}
		return bitmap;
	} 
	
	public void release(){
		for(Entry<String,SoftReference<Bitmap>> entry:map.entrySet()){
			if(entry.getValue()!=null){
				entry.getValue().get().recycle();
			}
		}
	}
}
